﻿@{
    Layout = "~/Views/Shared/_LayoutAdminPage.cshtml";
}

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">名&emsp;称:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input" placeholder="输入名称" />
                        </div>
                    </div>

                    <div class="layui-inline">
                        &emsp;
                        <button class="layui-btn icon-btn" lay-filter="linkTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="linkTable" lay-filter="linkTable"></table>
        </div>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="linkTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>



<!-- 表单弹窗 -->
<script type="text/html" id="linkEditDialog">
    <form id="linkEditForm" lay-filter="linkEditForm" class="layui-form model-form">
        <input name="id" value="0" type="hidden" />
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">名称:</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">地址:</label>
            <div class="layui-input-block">
                <input name="url" placeholder="请输入跳转地址" class="layui-input" value="http://" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">传递权重:</label>
            <div class="layui-input-block">
                <input type="radio" name="nofollow" value="0" title="是" checked />
                <input type="radio" name="nofollow" value="1" title="否" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">排序:</label>
            <div class="layui-input-block">
                <input name="sort" placeholder="请输入排序号" value="1" type="number" class="layui-input"
                       lay-verType="tips" lay-verify="required|number" required />
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">状态:</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="正常" checked />
                <input type="radio" name="status" value="0" title="禁用" />
            </div>
        </div>



        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="linkEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>


<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'xmSelect', 'notice', 'fileChoose'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var xmSelect = layui.xmSelect;
        var notice = layui.notice;
        var fileChoose = layui.fileChoose;


        /* 渲染表格 */
        var insTb = table.render({
            elem: '#linkTable',
            url: '/api/admin/Link/GetLinkList',
            //limit: 20, //默认展示数
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                { type: 'checkbox' },
                { field: 'id', width: 60, align: 'center', sort: true },
                { field: 'name', title: '名称', align: 'center' },
                { field: 'url', title: '跳转地址', align: 'center' },
                {
                    field: 'nofollow', title: '传递权重', templet: function (d) {
                        var strs = [
                            '<span class="text-success">是</span>',
                            '<span class="text-danger">否</span>'
                        ];
                        return strs[d.nofollow];
                    }, align: 'center', sort: true
                },
                { field: 'sort', title: '排序', sort: true },
                {
                    field: 'status', title: '状态', templet: function (d) {
                        var strs = [
                            '<span class="text-danger">禁用</span>',
                            '<span class="text-success">正常</span>'
                        ];
                        return strs[d.status];
                    }, align: 'center', sort: true
                },
                {
                    field: 'createTime', title: '创建时间', templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, sort: true
                },
                { title: '操作', toolbar: '#linkTbBar', align: 'center', minWidth: 200 }
            ]]
        });


        /* 表格搜索 */
        form.on('submit(linkTbSearch)', function (data) {
            insTb.reload({ where: data.field, page: { curr: 1 } });
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(linkTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(linkTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('linkTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', { icon: 2 });
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ ids: ids });
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '友情链接',
                content: $('#linkEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('linkEditForm', mData);

                    // 表单提交事件
                    form.on('submit(linkEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.post(mData ? '/api/admin/Link/EditLinkInfo' : '/api/admin/Link/AddLinkInfo', data.field, function (res2) {
                            layer.close(loadIndex);
                            if (1 == res2.code) {
                                layer.close(dIndex);
                                layer.msg(res2.message, { icon: 1 });
                                insTb.reload({ page: { curr: 1 } });
                            } else {
                                layer.msg(res2.message, { icon: 2 });
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post('/api/admin/Link/DeleteLink', {
                    ids: obj.data ? obj.data.id : obj.ids.join(',')
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code == 1) {
                        layer.msg(res.message, { icon: 1 });
                        insTb.reload({ page: { curr: 1 } });
                    } else {
                        layer.msg(res.message, { icon: 2 });
                    }
                }, 'json');
            });
        }

    });

</script>